//
//  NSArrayTests.m
//  iOS Sample Code
//
//  Created by lei yu on 11-12-8.
//  Copyright (c) 2011年 lancet. All rights reserved.
//

#import "NSArrayTests.h"

@implementation AddressBook

-(id)initWithName:(NSString *)name{
    self=[super init];
    if(self){
        array_ = [[NSMutableArray alloc]init];
        name_ = [name retain];
    }
    return self;
}

-(void)addCard:(AddressCard *)card{
    
    if([array_ containsObject:card]){
        return;
    }
    [array_ addObject:card];
    
}

-(NSInteger)list{
    
    return [array_ count];
}


-(AddressCard *) lookup: (NSString *) theName {
    NSUInteger result =(NSUInteger) [array_  indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
        if ([[obj name] caseInsensitiveCompare: theName] ==NSOrderedSame)
        {
            *stop = YES; // found a match, one is enough return YES;
            return YES;
        } else
        {
            return NO; // keep looking
        }
    }];
    // See if we found a match
    if (result != NSNotFound){ // there should only be one element 
        return [array_ objectAtIndex: result];}
    else{
        return nil;
    }
}


-(NSMutableArray *) lookupAll: (NSString *) theName {
    NSMutableArray *matches = [NSMutableArray array];
    NSIndexSet *result = [array_ indexesOfObjectsPassingTest: ^(id obj, NSUInteger idx, BOOL *stop)
    {
        if ([[obj name] caseInsensitiveCompare: theName] ==
            NSOrderedSame) {

            [matches addObject: obj];
            return YES; }
        else
            return NO;
    }];
    // See if we found a match
    if ([matches count]) return matches;
    else
        return nil;
}

-(void)sort{
    [   array_ sortUsingComparator:^NSComparisonResult(id book1,id book2){
        AddressCard *card = (AddressCard *)book1;
        AddressCard *card2 = (AddressCard *)book2;
        
        return [card.name compare:card2.name];
        
        
        
    } ];
}


-(void)print{
    for(AddressCard *card in array_){
        NSLog(@"%@",card);
    }
}
@end


@implementation AddressCard

@synthesize name = name_,email=email_;
-(void)setName:(NSString *)name andEmail:(NSString *)email{
    name_=[name retain];
    email_=[email retain];
}

-(void)dealloc{
    [name_ release];
    [email_ release];
    [super dealloc];
}

-(NSString *)description{
    return [[NSString alloc]initWithFormat:@"The name is %@ ,The email is%@",name_,email_];
}

@end


@implementation NSArrayTests


-(void) testNSArray{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSString *aName = @"Julia Kochan";
    NSString *aEmail = @"jewls337@axlc.com";
    NSString *bName = @"Tony Iannino";
    NSString *bEmail = @"tony.iannino@techfitness.com";
    NSString *cName = @"Stephen Kochan";
    NSString *cEmail = @"steve@classroomM.com";
    NSString *dName = @"Jamie Baker";
    NSString *dEmail = @"jbaker@classroomM.com";
    
    AddressCard *card1 = [[AddressCard alloc] init];
    AddressCard *card2 = [[AddressCard alloc] init];
    AddressCard *card3 = [[AddressCard alloc] init];
    AddressCard *card4 = [[AddressCard alloc] init];
    AddressBook *myBook = [AddressBook alloc];
    
    [card1 setName: aName andEmail: aEmail];
    [card2 setName: bName andEmail: bEmail]; 
    [card3 setName: cName andEmail: cEmail]; 
    [card4 setName: dName andEmail: dEmail];
    myBook = [myBook initWithName: @"Linda's"];
    [myBook addCard: card1];
    [myBook addCard: card2]; 
    [myBook addCard: card3];
    [myBook addCard: card4];
    
    [myBook sort]; 
    [myBook list];
    [myBook print];
    
    
    [card1 release]; 
    [card2 release];
    [card3 release];
    [card4 release];
    [myBook release]; 
    [pool drain]; 
    
    
    
    
    
    
}
@end
